/* ***********************
 * Version: MPL 1.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is WriteAds.js module.
 *
 * The Initial Developer of the Original Code is Rob Gerns.
 *
 * Portions created by the Initial Developer are Copyright (C) 2010
 * the Initial Developer. All Rights Reserved.
 *
 **************************/

/***********************************************
* Function Name(s): getAds,writeAds
*
* Purpose:
* Read in any advertisement elements from the
* Oister document, create the appropriate nodes
* and HTML to display the ads
************************************************/

function getAds(xmlDocument){
  
 //all of the advertisement elements in the Oister file in an array
 var adsCount = xmlDocument.getElementsByTagName("advertisement"); 
   
 //Only continue if there are actually advertisement elements in the Oister document
 if(adsCount != undefined && adsCount !=null){
	
  //Probably redundant, but double-check to make sure there is at least one ad element
	if(adsCount.length > 0){
			
	 //Set up a loop to read all of the ads and associated attributes
   for(var i=0; i < adsCount.length; i++){
		
		//The text information of the advertisement element   		
		var adText = adsCount[i].firstChild.nodeValue; 
		
		//check to see if the "adlink" attribute was set, if so get the value
		if(adsCount[i].getAttribute("adlink")!= null && adsCount[i].getAttribute("adlink")!=undefined){
		
		 var adLink = adsCount[i].getAttribute("adlink");
	  
	  }
			
		//otherwise make the variable blank
		else adLink="";
				
	  //check to see if the "image" attribute was set, if so get the value
		if(adsCount[i].getAttribute("image")!= null && adsCount[i].getAttribute("image")!=undefined){
		
		 var adImage = adsCount[i].getAttribute("image");
		
		}
				
		//otherwise make the variable blank
		else adImage="";
				
		//check to see if the "caption" attribute was set, if so get the value
		if(adsCount[i].getAttribute("caption")!= null && adsCount[i].getAttribute("caption")!=undefined){
		 
		 var adCaption = adsCount[i].getAttribute("caption");
		
		}
		
		//otherwise make the variable blank
		else adCaption = "";
				
		//check to see if the "hide" attribute was set, if so get the value
		if(adsCount[i].getAttribute("hide")!= null && adsCount[i].getAttribute("hide")!=undefined){
		 
		 var adHide = adsCount[i].getAttribute("hide");
		 
		} 
		
		//otherwise make the variable blank
		else var adHide="";
				
	  //Call the function that will actually manipulate the DOM, pass all the variables, including the current index of this ad
		writeAds(i, adText, adLink, adImage, adCaption, adHide);
		
   }
  
  }
 
 }

}


function writeAds(i, adText, adLink, adImage, adCaption, adHide){
 
 //used to make the ID of each ad match the already created ad nodes   
 var j = i+1;  var adId = "advertisement"+j;

 document.getElementById(adId).className="advertisement"; //set the class of the node
	
 //If the ad "hide" attribute is set to true, then don't display it
 if(adHide == "true"){
  
  document.getElementById(adId).style.display="none";
 
 }
	
 //Otherwise write the appropritate HTML depending on which attributes are set/not set
 else if(adHide != "true" && adImage != "" && adCaption == ""){

  document.getElementById(adId).innerHTML=""+adText+"<br><a href=\""+adLink+"\"><img src=\""+adImage+"\"/></a>";
 }
	
 else if(adHide != "true" && adImage != "" && adCaption != ""){
	
  document.getElementById(adId).innerHTML=""+adText+"<br><a href=\""+adLink+"\"><img src=\""+adImage+"\" alt=\""+adCaption+"\"/></a>";
 }
	
 else if(adHide != "true" && adImage == "" && adCaption == "" ){
	
	document.getElementById(adId).innerHTML=""+adText+"<br><a href=\""+adLink+"\">"+adLink+"</a>";
 }
	
 else if(adHide != "true" && adImage == "" && adCaption != ""){
	
  document.getElementById(adId).innerHTML=""+adText+"<br><a href=\""+adLink+"\"title=\""+adCaption+"\">"+adLink+"</a>";
 }
	
}	
	
	